package com.itpeng.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itpeng.Utils.QiNiuUtils;
import com.itpeng.constant.MessageConstant;
import com.itpeng.entity.PageResult;
import com.itpeng.entity.QueryPageBean;
import com.itpeng.entity.Result;
import com.itpeng.pojo.Setmeal;
import com.itpeng.service.SetMealService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @Aythor:彭彦钦
 * @Data: 2020/9/21  20:29
 */
@RestController
@RequestMapping("/setMeal")
public class SetMealController {

    @Reference
    private SetMealService setMealService;

    /**
     * 分页条件查询
     * @param queryPageBean
     * @return
     */
    @PostMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult<Setmeal> setmeals = setMealService.findPage(queryPageBean);

        return new Result(true, MessageConstant.QUERY_SETMEALLIST_SUCCESS, setmeals);
    }

    /**
     * 上传文件到七牛云，并将地址响应给前端
     * @param imgFile
     * @return
     */
    @PostMapping("/upload")
    public Result upload(MultipartFile imgFile){
        //获取文件的全名称(文件名+后缀)
        String originalFilename = imgFile.getOriginalFilename();
        //获取文件的后缀
        String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
        //生成唯一的文件名
        String fileName = UUID.randomUUID().toString() + substring;
        try {
            //将上传来的文件字节流传入七牛云
            QiNiuUtils.uploadViaByte(imgFile.getBytes(), fileName);
            //将文件名及后缀存入Map集合
            Map<String, String> map = new HashMap<>();
            map.put("imgName", fileName);
            map.put("domain",QiNiuUtils.DOMAIN);
            //将Map封装后返回给前端
            return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS, map);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
    }

    /**
     * 添加套餐
     * @param setmeal
     * @param checkGroupIds
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Setmeal setmeal, Integer[] checkGroupIds){

         setMealService.add(setmeal, checkGroupIds);

        return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
    }

    /**
     * 根据ID删除套餐
     * @return
     */
    @GetMapping("/deleteById")
    public Result deleteById(Integer id){
        setMealService.deleteById(id);

        return new Result(true, MessageConstant.DELETE_SETMEAL_SUCCESS);
    }

    /**
     * 根据ID查找套餐
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public Result findById(Integer id){
        //查找套餐
        Setmeal setmeal = setMealService.findById(id);
        //创建Map集合将套餐对象和图片网址前缀存入,并返回给前端
        HashMap<String, Object> hashMap = new HashMap<String, Object>();
        hashMap.put("setmeal", setmeal);
        hashMap.put("domain", QiNiuUtils.DOMAIN);

        return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, hashMap);
    }

    /**
     * 根据套餐ID查询对应的检查组
     * @param id
     * @return
     */
    @GetMapping("/findCheckgroupIdsBySetmealId")
    public Result findCheckgroupIdsBySetmealId(Integer id){
       List<Integer> integerList = setMealService.findCheckgroupIdsBySetmealId(id);

       return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, integerList);
    }

    /**
     * 修改套餐
     * @param setmeal
     * @param checkgroupIds
     * @return
     */
    @PostMapping("/update")
    public Result update(@RequestBody Setmeal setmeal, Integer[] checkgroupIds){
        setMealService.update(setmeal, checkgroupIds);

        return new Result(true, MessageConstant.EDIT_SETMEAL_SUCCESS);
    }

}
